package com.tehmur.android.menuTest;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BasicTasbi extends Activity implements OnClickListener {
	
	EditText setTarget;
	int target;
	
	Button buttonIncrement,buttonReset,buttonResetRecord;
	int counter = 0;
	
	TextView count,record;
	
	Vibrator vib;
	
	//ScrollView scrollRecords;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
       
        //Store UI in memory
        setTarget = (EditText)findViewById(R.id.EditTextSetTarget);           
        buttonIncrement = (Button)findViewById(R.id.buttonIncrement); 
        buttonReset = (Button)findViewById(R.id.buttonReset);
        buttonResetRecord = (Button)findViewById(R.id.buttonResetRecord);
        count = (TextView)findViewById(R.id.TextViewCounter);
        record = (TextView)findViewById(R.id.TextViewRecord);
        
        // Get instance of Vibrator from current Context
        vib = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

        
        //record.setMovementMethod(new ScrollingMovementMethod());
        
        //Define button listeners
        buttonIncrement.setOnClickListener(this);
        buttonReset.setOnClickListener(this);
        buttonResetRecord.setOnClickListener(this);

        //setTarget.setInputType(target);
        //scrollRecords = (ScrollView)findViewById(R.id.ScrollView01);
        //scrollRecords.addView(record);
        
        //Initialise textView to be empty
        record.setText("");
                      
        
    }

	@Override
	public void onClick(View src) {
		switch (src.getId()) {
		case R.id.buttonIncrement:
			//Gets input from the editText
			target = Integer.parseInt(setTarget.getText().toString());
			if ( counter == target ) {	
				// Vibrate for 800 milliseconds
				 vib.vibrate(800);
			}
			else {
				counter += 1;
				count.setText(String.valueOf(counter));
				vib.vibrate(060);
				if ( counter == target ) {	
					// Vibrate for 800 milliseconds
					 vib.vibrate(800);
					 
					 //This appends text to the textView record, and
					 //gets a new line within the textView to put the text,
					 //so all new text is in the same textView
					 record.append("\nYou have completed 1 set of " + target);
				}
			}
			break;
		case R.id.buttonReset:
			counter = 0;
			count.setText(String.valueOf(counter));
			break;
		case R.id.buttonResetRecord:
			record.setText("");
			break;
		}
	}
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.icontext01:     Toast.makeText(this, "You have reset the counter", Toast.LENGTH_SHORT).show();
			counter = 0;
			count.setText(String.valueOf(counter));                   
	        break;
	       
	        case R.id.icontext02: Toast.makeText(this, "You have reset the record", Toast.LENGTH_SHORT).show();
	        record.setText("");                    
	        break;
	    }
	    return true;
	}
	
	
}